//
//  ZBaseViewController.m
//  ZPlayer
//
//  Created by Zed Link on 26/11/2016.
//  Copyright © 2016 ZED. All rights reserved.
//

#import "ZBaseViewController.h"

@interface ZBaseViewController () <UIGestureRecognizerDelegate>

@end

@implementation ZBaseViewController

#pragma mark - 快捷功能

+ (instancetype)zNew
{
    return [[self alloc] init];
}

+ (instancetype)zStoryboardInstance
{

    ZBaseViewController *vc = [[UIStoryboard storyboardWithName:NSStringFromClass([self class]) bundle:nil] instantiateViewControllerWithIdentifier:NSStringFromClass(self.class)];
    NSAssert([vc isKindOfClass:[ZBaseViewController class]], @"从Storyboard加载的控制器并非ZBaseViewController的子类");
    
    return vc;
}

+ (instancetype)zStoryboardInstanceWithStoryboardName:(NSString *)sbName{
    
    ZBaseViewController *vc = [[UIStoryboard storyboardWithName:sbName bundle:nil] instantiateViewControllerWithIdentifier:NSStringFromClass(self.class)];
    NSAssert([vc isKindOfClass:[ZBaseViewController class]], @"从Storyboard加载的控制器并非ZBaseViewController的子类");
    
    return vc;
}

#pragma mark - About Navigation

- (void)zAddBackItemWithGesture:(BOOL)gestureSupport;
{
    // 由于自定义了返回按钮, 需要额外配置interactivePop手势代理, 才能使左划返回手势生效
    if (gestureSupport == YES) {
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
    
    self.navigationItem.leftBarButtonItem = [self zNavItemWithImage:@"icon_return" shouldOriginal:YES andSelector:@selector(zPopBack)];
}

- (void)zAddWhiteBackItemWithGesture:(BOOL)gestureSupport
{
    // 由于自定义了返回按钮, 需要额外配置interactivePop手势代理, 才能使左划返回手势生效
    if (gestureSupport == YES) {
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
    
    self.navigationItem.leftBarButtonItem = [self zNavItemWithImage:@"icon_return_white" shouldOriginal:YES andSelector:@selector(zPopBack)];
}

- (UIBarButtonItem *)zNavItemWithImage:(NSString *)imageName shouldOriginal:(BOOL)original andSelector:(SEL)selector
{
    UIImage *image = [UIImage imageNamed:imageName];
    if (original) {
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    
    if (image == nil) {
        ZLog(@"找不到NavigationItem对应的图片");
    }
    
    return [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:selector];
}

- (UIBarButtonItem *)zNavItemWithTitle:(NSString *)title andSelector:(SEL)selector
{
    UIButton *rightBtn = [UIButton zButtonWithTitle:title fontSize:16 textColor:FONT_THIRD_COLOR];
    [rightBtn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

- (void)zPopBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)zClearNavBgColorAndSeparator:(BOOL)hasSeparator
{
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    if (hasSeparator == NO) {
        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    }
}

- (void)zSetNavigationForceColor:(UIColor *)color
{
    [self.navigationController.navigationBar setTintColor:color];
    [self.navigationController.navigationBar setTitleTextAttributes:@{
                                                                      NSForegroundColorAttributeName : color
                                                                      }];
}

#pragma mark - 统一规则定义

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 设置导航条背景颜色
     [self.navigationController.navigationBar setBackgroundImage:[UIImage zImageWithColor:SEPARATOR_BG_COLOR_Low size:CGSizeMake(Z_SCREEN_WIDTH, 64) andRadius:1] forBarMetrics:UIBarMetricsDefault];
 
    self.automaticallyAdjustsScrollViewInsets = NO;
    // 设置View的背景颜色
    [self.view setBackgroundColor:VIEW_BG_White_COLOR];
    
  
 
    [self zConfigNavigation];
    [self zLoadSubview];

}

- (void)zConfigNavigation
{
//    self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName : [UIFont systemFontOfSize:18 weight:UIFontWeightSemibold]};

    
}

- (void)zLoadSubview
{
}

#pragma mark - Getter & Setter

- (UIImageView *)navBgView
{
    if (_navBgView == nil) {
        // ========== Navigation背景图 ==========
        _navBgView = [[UIImageView alloc] init];
        _navBgView.userInteractionEnabled = YES;
        
        [self.view addSubview:_navBgView];
        [_navBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.mas_equalTo(self.view);
            make.height.mas_equalTo(64);
        }];
    }
    return _navBgView;
}

- (UIImageView *)navSeparatorLine
{
    if (_navSeparatorLine == nil) {
        // ========== Navigation分割线 ==========
        _navSeparatorLine = [[UIImageView alloc] init];
        
        [self.navBgView addSubview:_navSeparatorLine];
        [_navSeparatorLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(self.navBgView);
            make.height.mas_equalTo(1);
        }];
    }
    return _navSeparatorLine;
}

- (void)dealloc
{
    ZLog(@"%@ is dealloc", NSStringFromClass(self.class));
}

@end
